package com.netease.homework.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.netease.homework.dao.IUserDao;
import com.netease.homework.meta.User;
import com.netease.homework.service.ILoginService;

@Component
@Qualifier("LoginService")
public class LoginServiceImpl implements ILoginService {

	@Autowired
	private IUserDao userDao;
	
	@Override
	public Map<String, Object> login(String name, String password) throws Exception {
		Map<String,Object> returnMap = new HashMap<String,Object>();

        User user = new User();
        try {
            user = userDao.getUserByName(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(user != null ){
            if(user.getPassword().equals(password)){
                returnMap.put("value", user);
                returnMap.put("message", "登录成功");
                returnMap.put("success", true);
            }else{
                returnMap.put("message", "密码错误");
                returnMap.put("success", false);
            }
        }else {
            returnMap.put("message", "该用户不存在");
            returnMap.put("success", false);
        }
        return returnMap;
	}

	@Override
	public Map<String, Object> getLoger(Integer id) throws Exception {
		Map<String,Object> returnMap = new HashMap<String,Object>();

        User user = userDao.getUserById(id);
        if(user != null){
            returnMap.put("value", user);
            returnMap.put("message", "获取成功");
            returnMap.put("success", true);

        }else{
            returnMap.put("message", "该用户不存在!");
            returnMap.put("success", false);
        }
        return returnMap;
	}

	@Override
	public Map<String, Object> register(String name, String password) {
		 Map<String,Object> returnMap = new HashMap<String,Object>();

	        User user = new User();
	        if(userDao.getUserByName(name) != null){
	            returnMap.put("message", "该账号已存在...");
	            returnMap.put("success", false);
	            return returnMap;
	        }else{
	            user.setName(name);
	            user.setPassword(password);

	            userDao.save(user);
	            user.setId(userDao.getUserByName(name).getId());
	            returnMap.put("value", user);
	            returnMap.put("message", "注册成功");
	            returnMap.put("success", true);
	            return returnMap;
	        }
	}

	
}
